<?php
class Mapinfo extends AdminAction{
	function showKey($key){
		$db=$this->getDB();

		$entity = $db->get_assoc("select * from mapinfo where lang = '".$this->lang."' and keyid = '$key'");
		$this->data['entity'] = $entity;
		$this->data['key'] = $key;

		template('mapinfo',$this->data);
	}

	function updatekey(){
		$id = P('id');
		$key = P('key');
		$content = P('content');
		$content = addslashes($content);

		$db=$this->getDB();
		$db->query("update mapinfo set content = '$content' where id = $id");

		message('内容编辑成功！', '/admin.php/mapinfo/showKey/'.$key);

	}

	function showsalenet(){
		$db=$this->getDB();

		$salenet = $db->get_assoc("select * from mapinfo where keyid = 'salenet' and lang = '".$this->lang."'");
		$this->data['salenet'] = $salenet;
		/*
		$saleid = $salenet['id'];
		$imglist = $db->query_assoc("select id,location from mapinfo_images where mapinfoid = $saleid order by createtime asc");

		$imgids = "";
		if(sizeof($imglist)>=1){
			$i = 1;
			foreach ($imglist as $img){
				if($i == sizeof($imglist)){
					$imgids .=$img['id'];
				}else{
					$imgids .=$img['id'].',';
				}
			}
		}*/
		$this->data['imgids'] = $imgids;
		$this->data['imglist'] = $imglist;
		template('salenet',$this->data);
	}
	
	function getSalenetImgList($saleid){
		$db=$this->getDB();
		$result = array();
		$imglist = $db->query_assoc("select id,location,createtime from mapinfo_images where mapinfoid = $saleid order by createtime asc");
		$result['total'] = sizeof($imglist);
		$result['rows'] = $imglist;
		
		echo json_encode($result);
		
	}

	function addsaleimg(){
		$db=$this->getDB();
		$mapinfoid = P('mapinfoid');

		$file = $this->loadLib('file');
		$filename = $file->uploadfile('saleimg','attach/salenet');

		$db->query("insert into mapinfo_images (location,createtime,mapinfoid) values ('$filename',now(),'$mapinfoid')");
		$insertid = $db->insert_id();

		$message = array();
		$message['insertid'] = $insertid;
		$message['location'] = $filename;
		$message = setMessage(true,'上传成功！',$message);
		echo json_encode($message);
	}

	function updatesalenet(){
		$db=$this->getDB();
		$sid = P('id');
		$attachids = P('attachids');
		$content = P('content');
		$content = addslashes($content);

		$ids = array();
		if($attachids!=""){
			if(strpos($attachids, ',')){
				$ids = explode(',',$attachids);
			}else{
				$ids =$attachids;
			}

			if(is_array($ids)){
				if(sizeof($ids)>=1){
					foreach ($ids as $id){
						if($id!=""){
							$db->query("update mapinfo_images set mapinfoid = $sid where id = $id");	
						}
					}
				}
			}else{
				$db->query("update mapinfo_images set mapinfoid = $sid where id = $ids");		
			}

		}
		
 		$db->query("update mapinfo set content = '$content' where id  = $sid");
		message('销售网络编辑成功！', '/admin.php/mapinfo/showsalenet');
	}

	function imgdelete(){
		$imgid = P('imgid');
		
		$db=$this->getDB();
		
		$imgid = P('imgid');
		$saleimg = $db->get_assoc("select * from mapinfo_images where id = $imgid");
		
		$file = $this->loadLib('file');
		$file ->unlink($saleimg['location']);
		
		$db->query("delete from mapinfo_images where id = $imgid");
		
		$message = array();
		$message = setMessage(true,'文件删除成功！',$message);
		echo json_encode($message);
	}
	
	function getmapimages($key){
		$db=$this->getDB();
		$result = array();
		$imglist = $db->query_assoc("select id,location,createtime from info_images where infotype = '$key' order by createtime asc");
		$result['total'] = sizeof($imglist);
		$result['rows'] = $imglist;
		
		echo json_encode($result);
	}
	
	function addinfoimg(){
		$db=$this->getDB();
		$key = P('key');

		$file = $this->loadLib('file');
		$filename = $file->uploadfile('saleimg','attach/info');

		$db->query("insert into info_images (location,createtime,infotype) values ('$filename',now(),'$key')");
		$insertid = $db->insert_id();

		$message = array();
		$message['insertid'] = $insertid;
		$message['location'] = $filename;
		$message = setMessage(true,'上传成功！',$message);
		echo json_encode($message);
	}
	
	function deleteimg(){
		$imgid = P('imgid');
		
		$db=$this->getDB();
		
		$imgid = P('imgid');
		$saleimg = $db->get_assoc("select * from info_images where id = $imgid");
		
		$file = $this->loadLib('file');
		$file ->unlink($saleimg['location']);
		
		$db->query("delete from info_images where id = $imgid");
		
		$message = array();
		$message = setMessage(true,'文件删除成功！',$message);
		echo json_encode($message);
	}
	
}

